package com.example.backetballgame.gui;

import com.example.backetballgame.store.StoreManage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * The TeamSeasonFrame class represents the GUI for choosing the team name and season for the game.
 * It extends the JFrame class and implements the KeyListener and ActionListener interfaces.
 */
public class TeamSeasonFrame extends JFrame implements KeyListener, ActionListener {

    private JTextField teamNameField; // Team name input field
    private JTextField seasonField; // Season time input field
    private JButton backButton; // Back button
    private JButton continueButton; // Continue button

    /**
     * Constructor for the TeamSeasonFrame class. Initializes the GUI components and displays the window.
     */
    public TeamSeasonFrame() {
        super("Choose Team Name and Season"); // Set window title
        setSize(800, 600); // Set window size
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set default operation when closing window

        // Create panel and layout manager
        JPanel panel = new JPanel(new GridBagLayout());

        // Create labels and input fields
        JLabel teamNameLabel = new JLabel("Team Name:");
        teamNameField = new JTextField(20);
        JLabel seasonLabel = new JLabel("Season Time:");
        seasonField = new JTextField(20);

        // Add components to panel and set constraint relationships
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NONE;
        c.weightx = 1;
        c.weighty = 1;
        c.gridx = 0;
        c.gridy = 0;
        panel.add(teamNameLabel, c);
        c.gridx = 1;
        c.gridy = 0;
        panel.add(teamNameField, c);
        c.gridx = 0;
        c.gridy = 1;
        panel.add(seasonLabel, c);
        c.gridx = 1;
        c.gridy = 1;
        panel.add(seasonField, c);

        // Create buttons
        backButton = new JButton("Back");
        continueButton = new JButton("Continue");

        // Set event listeners for buttons
        backButton.addActionListener(this);
        continueButton.addActionListener(this);

        // Add buttons to panel and set constraint relationships
        c.gridx = 0;
        c.gridy = 2;
        panel.add(backButton, c);
        c.gridx = 1;
        c.gridy = 2;
        panel.add(continueButton, c);

        // Add keyboard event listener
        addKeyListener(this);
        setFocusable(true);

        // Add panel to the center of the window
        getContentPane().add(panel, BorderLayout.CENTER);

        setVisible(true); // Show window
    }

    /**
     * Handles key press events. Pressing the space key enters the game interface.
     *
     * @param e the KeyEvent object
     */
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            dispose(); // Close the window
            new BattlePreparationFrame(); // Enter the game interface
        }
    }

    /**
     * Handles key typed events.
     *
     * @param e the KeyEvent object
     */
    @Override
    public void keyTyped(KeyEvent e) {
    }

    /**
     * Handles key released events.
     *
     * @param e the KeyEvent object
     */
    @Override
    public void keyReleased(KeyEvent e) {
    }

    /**
     * Handles action events. Clicking the Back button returns to the welcome page.
     * Clicking the Continue button initializes the game configuration and enters the game interface.
     *
     * @param e the ActionEvent object
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == backButton) {
            dispose(); // Close the window
            new WelcomeFrame(); // Return to the welcome page
        } else if (e.getSource() == continueButton) {
            String errMsg = checkFormValid(teamNameField.getText(), seasonField.getText());
            if (errMsg != null) {
                JOptionPane.showMessageDialog(null, errMsg);
                return;
            }
            dispose(); // Close the window
            StoreManage.initConfig(teamNameField.getText(), seasonField.getText());
            new BattlePreparationFrame(); // Enter the game interface
        }
    }

    /**
     * The checkFormValid method checks if the user input in the team name and season fields are valid.
     *
     * @return an empty string if the input is valid, otherwise returns an error message
     */
    public String checkFormValid(String teamName, String season) {
        if (teamName == null || teamName.equals("")) {
            return "Please enter team name";
        }
        if (season == null || season.equals("")) {
            return "Please enter season time";
        }
        return null;
    }

    /**
     * The main method creates a new object of the TeamSeasonFrame class.
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        TeamSeasonFrame teamSeasonPage = new TeamSeasonFrame(); // Create window object
    }
}